package com.bazaaroid.client.web.gwt.clientmodule.client.view.search.impl;

import java.util.Set;

import com.bazaaroid.client.web.gwt.clientmodule.client.BazaaroidRestService;
import com.bazaaroid.client.web.gwt.clientmodule.client.view.search.ClientSearch;
import com.bazaaroid.client.web.gwt.clientmodule.client.presenter.Presenter;
import com.google.gwt.core.client.GWT;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.uibinder.client.UiBinder;
import com.google.gwt.uibinder.client.UiField;
import com.google.gwt.uibinder.client.UiHandler;
import com.google.gwt.uibinder.client.UiTemplate;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.Hyperlink;
import com.google.gwt.user.client.ui.ListBox;
import com.google.gwt.user.client.ui.VerticalPanel;
import com.google.gwt.user.client.ui.Widget;

public class ClientSearchImpl extends Composite implements
		ClientSearch {

	@UiTemplate("ClientSearch.ui.xml")
	interface ClientSearchUiBinder extends UiBinder<Widget, ClientSearchImpl> {
	}

	private static ClientSearchUiBinder uiBinder = GWT.create(ClientSearchUiBinder.class);

	private Presenter presenter;
	
	@UiField Hyperlink detailedSearchLink;
	@UiField ListBox categoriesListBox;
	@UiField VerticalPanel detailedSearchPanel;
	
	public ClientSearchImpl() {
		initWidget(uiBinder.createAndBindUi(this));
		Set<String> categories = BazaaroidRestService.getCategories();
		for(String category : categories) {
			categoriesListBox.addItem(category);
		}
		detailedSearchPanel.setVisible(false);
	}
	
	public void setPresenter(Presenter presenter) {
		this.presenter = presenter;
	}

	public Widget asWidget() {
		return this;
	}

	@UiHandler("detailedSearchLink")
	  void onDetailedSearchLinkClicked(ClickEvent event) {
		if (detailedSearchPanel.isVisible() == true) {
			detailedSearchPanel.setVisible(false);
		} else {
			detailedSearchPanel.setVisible(true);
		}
			
	  }
}
